package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import models.RatingMPS;
import models.Recommend;
import models.SolutionSubmission;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class ajxcheckExpertValidationServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		PrintWriter out = resp.getWriter();
		try{
			resp.setContentType("text/xml");
			
	        UserService userService = UserServiceFactory.getUserService();
	        String User_EmailID = userService.getCurrentUser().getEmail(); 
			
			String SolID = req.getParameter("sol_id");
			if ( SolID == null ){
				resp.sendRedirect("/home.jsp?error=Solution id is null.");
			}
			Long lSolID = Long.parseLong(SolID);
			
			//CHECK FOR EXISTING RECOMMENDATION
			Objectify ofy = ObjectifyService.begin();
			Query<Recommend> q = ofy.query(Recommend.class).filter("solID", lSolID).filter("recommendBy", User_EmailID);
	   		for ( Recommend f : q.fetch() ) {
	   			out.println("already recommended");
	   			return;
	   		}			
	   		out.println("not recommended");
	   		
		} catch(Exception ee){
			ee.printStackTrace();
			out.println("not recommended");
		}
	}
}
